package com.niit.mall.common.interceptors;


import cn.hutool.core.util.StrUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import com.niit.mall.common.utils.UserContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @BelongsProject: pardo_mall_backend
 * @BelongsPackage: com.niit.mall.common.interceptors
 * @Author: ChuXuan
 * @CreateTime: 2025-05-29  01:09
 * @Description: 用户信息拦截器
 * @Version: 1.0
 */

public class UserInfoInterceptors implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 1.获取请求头中的用户信息
		String userInfo = request.getHeader("user-info");
		System.out.println(userInfo);
		// 2.判断是否为空
		if (StrUtil.isNotBlank(userInfo)) {
			// 不为空，保存到ThreadLocal
			UserContext.setUser(Long.valueOf(userInfo));
		}
		// 3.放行
		return true;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// 移除用户
		UserContext.removeUser();
	}
}
